﻿/*
	初始化
*/
window.g_placeName = "";	//当前的地点名称
window.g_x = 0;				//经度
window.g_y = 0;				//纬度
window.map = 0;				//百度地图
window.g_airDir = 0;		//风向
window.g_flySpeed = 0;		//飞行器的速度
window.g_signal = 0;		//信号强度
window.g_power = 0;			//电量
window.g_time = 0;			//子系统启动之后的时间
window.g_pitchAngle = 0;	//仰俯角度
window.g_health = 0;		//健康值
window.g_airSpeed = 0;      //风速
window.g_interval = 0;      //定时器
window.g_startNum = 0;      //卫星数量
window.g_distance = 0;      //距离
window.g_seil = 0;          //航程
window.g_startPoint = 0;	//起点
window.g_endtPoint = 0; 	//终点
window.g_myicon = 0;        //ico图标
window.g_plane = 0;         //飞机标记

function onTimeRun() {
    $("#signal").html("信号强度:" + g_signal.toString() + "%");
    $("#power").html("剩余电量:" + g_power.toString() + "%");
    $("#time").html("飞行时间:" + g_time.toString() + "ms");
    $("#stars").html("卫星数量:" + g_startNum.toString());
    //$("#dis").html("距离:" + g_distance.toString() + "m");
    //$("#sail").html("航程" + g_seil.toString() + "m");
    if (0 != g_myicon) {
        if (0 != g_plane) {
            map.removeOverlay(g_plane);
        }
        var pt = new BMap.Point(g_x, g_y);
        g_plane = new BMap.Marker(pt, { icon: myIcon });  // 创建标注
        map.addOverlay(g_plane);              // 将标注添加到地图中
    }
}

function SetParam(seil, dis, num, x, y, str, flyspeed,
 signal, power, time, airspeed, angle, value, dir) {
    SetSeil(seil);
    SetDistance(dis);
    SetStarNum(num);
    SetPositionByCoordinate(x, y);
    SetPositionByPlaceName(str);
    SetFlyerSpeed(speed);
    SetSignalStrength(signal);
    SetPowerPercent(power);
    SetTime(time);
    SetAirSpeed(speed);
    SetPitchAngle(angle);
    SetHealthValue(value);
    SetAirDir(dir);
}

function SetSeil(seil) {    //设置航程
    g_seil = seil;
}

function SetDistance(dis) {                 //设置距离
    g_distance = dis;
}

function SetStarNum(num)                   //设置卫星的数量
{
    g_startNum = num;
}

function SetPositionByCoordinate(x, y)	//根据经纬度设置地图位置
{
    g_x = x / Math.Pow(10,7);
    g_y = y / Math.Pow(10, 7);
}

function SetPositionByPlaceName(str)	//根据名称设置地理位置 并把地图放大到顶级
{
	g_placeName = str;
}
function SetFlyerSpeed(speed)			//设置飞信器的速度 显示到左下角表盘
{
	g_flySpeed = speed;
}

function SetSignalStrength(signal)		//设置信号强度	显示左上角
{
	g_signal = signal	
}

function SetPowerPercent(power)		//设置电量		显示在右上角
{
	g_power = power;
	
}

function SetTime(time)				//显示到右上角
{
	g_time = time;	
}

function SetAirSpeed(speed)			//空气的速度		显示到左下角的小表盘中
{
	g_airDir = speed;
}


function SetPitchAngle(angle)			//设置俯仰角		显示到右下角的小表盘中
{
	g_pitchAngle = angle *180 / Math.PI;
}


function SetHealthValue(value)		//设置健康值		显示到右下角的大表盘中
{
    g_health = value + Math.round((Math.random() - 0.5) * 30);
}


function SetAirDir(dir)				//设置风向		暂时不用显示
{
    g_airDir = dir;
}

$(function () {
        myIcon = new BMap.Icon("smalllogo.png", new BMap.Size(32, 32)); 
		DrawTable();
		initialize();
        var myDate=new Date();
        $("#date").html("当前日期:" + myDate.toDateString());               
});

//*
function initialize() {
     map = new BMap.Map("allmap");
     map.centerAndZoom(new BMap.Point(116.036295, 28.695079), 11);    //"南昌",g:"115.893528,28.689578|12
     map.setCurrentCity("南昌");              //设置南昌为地图中心
     map.addControl(new BMap.MapTypeControl());//添加地图类型的控件
     map.enableScrollWheelZoom(true);           //支持鼠标滚动缩放
     var point = new BMap.Point(116.036295, 28.695079);//南昌工程学院
     //var marker = new BMap.Marker(point);        // 创建标注   
     //map.addOverlay(marker);                     // 将标注添加到地图中
     map.setZoom(19);                            //放大到顶级

     var marker2 = new BMap.Marker(point, { icon: myIcon });  // 创建标注
     map.addOverlay(marker2);              // 将标注添加到地图中
	
	//点击地图的事件
	 map.addEventListener("click",function(e) {
	 var sContent =
	"<input id = 'start_point' type='button' value='设置为起点'/>" +
	"<input id = 'end_point' type='button' value='设置为终点'/>";
	//+"<input id = 'add_point' type='button' value='新增中间点'/>";
    var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
	infoWindow.setTitle("提示");
	infoWindow.enableAutoPan();	//开启地图自动移动到信息框
	infoWindow.addEventListener("open", function(){
		$("#start_point").bind("click", e.point, StartPoint);
		$("#end_point").bind("click", e.point, EndPoint);
	});
    map.openInfoWindow(infoWindow, e.point);
});
   g_interval = setInterval(onTimeRun, 500);   //定时的设置

    /*
    var ac_start = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
        ,"location" : map
    });
    var ac_end = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" : "end"
        ,"location" : map
    });    
    doSearch(ac_start,map);
    doSearch_end(ac_end,map);
    */
}

//画出航线
function drawAirLine()
{
	if(0 != g_startPoint && 0 != g_endtPoint)
	{
		var polyline = new BMap.Polyline([g_startPoint,g_endtPoint], 
		{strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});  //定义折线
        map.addOverlay(polyline);
	}
}

//设置为终点
function EndPoint(point)	
{	var str = "hehe";
	map.closeInfoWindow();	
	if(0 == g_endtPoint)
	{
		str = "设置成功";
		//addPoint(2,point.data.lng*Math.pow(10,7), point.data.lat*Math.pow(10,7));
		g_endtPoint = new BMap.Point(point.data.lng, point.data.lat);
		var marker = new BMap.Marker(point.data);        // 创建标注 
		var opts = {
	  position : point,    // 指定文本标注所在的地理位置
	  offset   : new BMap.Size(10, -10)    //设置文本偏移量
	}
	var label = new BMap.Label("终点", opts);  // 创建文本标注对象
		label.setStyle({
			 fontSize : "12px",
			 height : "20px",
			 lineHeight : "20px",
			 fontFamily:"微软雅黑"
		 });
		marker.setLabel(label);
		marker.setTitle("终点");
		map.addOverlay(marker);                     // 将标注添加到地图中
		drawAirLine();
	}
	else
	{	
		str = "不能设置多个终点";
	}
	
	$.msgbox({
	closeImg: '',
	height:90,
	width:210,
	content:str,
	type :'text',
	bgAnimate: false, // 禁止背景动画
	boxAnimate: false, // 禁止提示框动画
	enableDrag: false // 禁止拖拽
});	
}

//设置为起点
function StartPoint(point)	
{	var str = "hehe";
	map.closeInfoWindow();	
	if(0 == g_startPoint)
	{
		str = "设置成功";
		//addPoint(1,point.data.lng*Math.pow(10,7), point.data.lat*Math.pow(10,7));
		g_startPoint = new BMap.Point(point.data.lng, point.data.lat);
		var marker = new BMap.Marker(point.data);        // 创建标注 
		var opts = {
	  position : point,    // 指定文本标注所在的地理位置
	  offset   : new BMap.Size(10, -10)    //设置文本偏移量
	}
	var label = new BMap.Label("起点", opts);  // 创建文本标注对象
		label.setStyle({
			 fontSize : "12px",
			 height : "20px",
			 lineHeight : "20px",
			 fontFamily:"微软雅黑"
		 });
		marker.setLabel(label);
		marker.setTitle("起点");
		map.addOverlay(marker);                     // 将标注添加到地图中
		drawAirLine();
	}
	else
	{	
		str = "不能设置多个起点";
	}
	
	$.msgbox({
	closeImg: '',
	height:90,
	width:210,
	content:str,
	type :'text',
	bgAnimate: false, // 禁止背景动画
	boxAnimate: false, // 禁止提示框动画
	enableDrag: false // 禁止拖拽
});	
}

function DrawTable()
{
	//飞行器的速度
	$('#container').highcharts({
        chart: {
            type: 'gauge',
            plotBackgroundColor: null,
            plotBackgroundImage: null,
            plotBorderWidth: 0,
            plotShadow: false,
			height:200,
			width:200,
			margin: [0, 0, 0, 0],
			backgroundColor: 'rgba(0,0,0,0)'
        },

        title: {
            text: '飞行速速'
        },

        pane: {
            startAngle: -150,
            endAngle: 150,
            background: [{
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#FFF'],
                        [1, '#333']
                    ]
                },
                borderWidth: 0,
                outerRadius: '109%'
            }, {
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#333'],
                        [1, '#FFF']
                    ]
                },
                borderWidth: 1,
                outerRadius: '107%'
            }, {
                // default background
            }, {
                backgroundColor: '#DDD',
                borderWidth: 0,
                outerRadius: '105%',
                innerRadius: '103%'
            }]
        },

        // the value axis
        yAxis: {
            min: 0,
            max: 200,

            minorTickInterval: 'auto',
            minorTickWidth: 1,
            minorTickLength: 10,
            minorTickPosition: 'inside',
            minorTickColor: '#666',

            tickPixelInterval: 30,
            tickWidth: 2,
            tickPosition: 'inside',
            tickLength: 10,
            tickColor: '#666',
            labels: {
                step: 2,
                rotation: 'auto'
            },
            title: {
                text: 'm/s'
            },
            plotBands: [{
                from: 0,
                to: 120,
                color: '#55BF3B' // green
            }, {
                from: 120,
                to: 160,
                color: '#DDDF0D' // yellow
            }, {
                from: 160,
                to: 200,
                color: '#DF5353' // red
            }]
        },
		credits: {
			enabled: false
		},
        series: [{
            name: 'Speed',
            data: [150],
            tooltip: {
                valueSuffix: ' m/s'
            }
        }]

    },
        //回调
        function (chart) {
            if (!chart.renderer.forExport) {
                setInterval(function () {
                    var point = chart.series[0].points[0],
                        newVal,
                        inc = Math.round((Math.random() - 0.5) * 20);

                    newVal = point.y - point.y + g_flySpeed;                  
                    point.update(newVal);
                }, 500);
            }
        });
		
		//健康值
		$('#Health').highcharts({
        chart: {
            type: 'gauge',
            plotBackgroundColor: null,
            plotBackgroundImage: null,
            plotBorderWidth: 0,
            plotShadow: false,
			height:200,
			width:200,
			margin: [0, 0, 0, 0],
			backgroundColor: 'rgba(0,0,0,0)'
        },

        title: {
            text: '健康值'
        },

        pane: {
            startAngle: -150,
            endAngle: 150,
            background: [{
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#FFF'],
                        [1, '#333']
                    ]
                },
                borderWidth: 0,
                outerRadius: '109%'
            }, {
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#333'],
                        [1, '#FFF']
                    ]
                },
                borderWidth: 1,
                outerRadius: '107%'
            }, {
                // default background
            }, {
                backgroundColor: '#DDD',
                borderWidth: 0,
                outerRadius: '105%',
                innerRadius: '103%'
            }]
        },

        // the value axis
        yAxis: {
            min: 0,
            max: 200,

            minorTickInterval: 'auto',
            minorTickWidth: 1,
            minorTickLength: 10,
            minorTickPosition: 'inside',
            minorTickColor: '#666',

            tickPixelInterval: 30,
            tickWidth: 2,
            tickPosition: 'inside',
            tickLength: 10,
            tickColor: '#666',
            labels: {
                step: 2,
                rotation: 'auto'
            },
            title: {
                text: ''
            },
            plotBands: [{
                from: 0,
                to: 120,
                color: '#55BF3B' // green
            }, {
                from: 120,
                to: 160,
                color: '#DDDF0D' // yellow
            }, {
                from: 160,
                to: 200,
                color: '#DF5353' // red
            }]
        },
		credits: {
			enabled: false
		},
        series: [{
            name: '健康值',
            data: [150],
            tooltip: {
                valueSuffix: ''
            }
        }]

    },
        // Add some life
        function (chart) {
            if (!chart.renderer.forExport) {
                setInterval(function () {
                    var point = chart.series[0].points[0],
                        newVal,
                        inc = Math.round((Math.random() - 0.5) * 20);

                    newVal = point.y - point.y + g_health;
                    point.update(newVal);
                }, 500);
            }
        });
		
		//风速
		$('#AirSpeed').highcharts({
        chart: {
            type: 'gauge',
            plotBackgroundColor: null,
            plotBackgroundImage: null,
            plotBorderWidth: 0,
            plotShadow: false,
			height:100,
			width:100,
			margin: [0, 0, 0, 0],
			backgroundColor: 'rgba(0,0,0,0)'
        },

        title: {
            text: '风速',
			y : 85
        },

        pane: {
            startAngle: -90,
            endAngle: 90,
            background: [{
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#FFF'],
                        [1, '#333']
                    ]
                },
                borderWidth: 0,
                outerRadius: '109%'
            }, {
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#333'],
                        [1, '#FFF']
                    ]
                },
                borderWidth: 1,
                outerRadius: '107%'
            }, {
                // default background
            }, {
                backgroundColor: '#DDD',
                borderWidth: 0,
                outerRadius: '105%',
                innerRadius: '103%'
            }]
        },

        // the value axis
        yAxis: {
            min: 0,
            max: 180,

            minorTickInterval: 'auto',
            minorTickWidth: 1,
            minorTickLength: 10,
            minorTickPosition: 'inside',
            minorTickColor: '#666',

            tickPixelInterval: 30,
            tickWidth: 2,
            tickPosition: 'inside',
            tickLength: 10,
            tickColor: '#666',
            labels: {
                step: 2,
                rotation: 'auto'
            },            
            plotBands: [{
                from: 0,
                to: 120,
                color: '#55BF3B' // green
            }, {
                from: 120,
                to: 160,
                color: '#DDDF0D' // yellow
            }, {
                from: 160,
                to: 180,
                color: '#DF5353' // red
            }]
        },
		credits: {
			enabled: false
		},
        series: [{
            name: 'Speed',
            data: [150],
            tooltip: {
                valueSuffix: ' m/s'
            }
        }]

    },
        // Add some life
        function (chart) {
            if (!chart.renderer.forExport) {
                setInterval(function () {
                    var point = chart.series[0].points[0],
                        newVal,                        
                    newVal = point.y - point.y + g_airSpeed
                    point.update(newVal);
                }, 500);
            }
        });
		
		//俯仰角-------------------------------------------
		$('#UpDownAngle').highcharts({
        chart: {
            type: 'gauge',
            plotBackgroundColor: null,
            plotBackgroundImage: null,
            plotBorderWidth: 0,
            plotShadow: false,
			height:100,
			width:100,
			margin: [0, 0, 0, 0],
			backgroundColor: 'rgba(0,0,0,0)'
        },

        title: {
            text: '俯仰角',
			y : 85
        },

        pane: {
            startAngle: -90,
            endAngle: 90,
            background: [{
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#FFF'],
                        [1, '#333']
                    ]
                },
                borderWidth: 0,
                outerRadius: '109%'
            }, {
                backgroundColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                    stops: [
                        [0, '#333'],
                        [1, '#FFF']
                    ]
                },
                borderWidth: 1,
                outerRadius: '107%'
            }, {
                // default background
            }, {
                backgroundColor: '#DDD',
                borderWidth: 0,
                outerRadius: '105%',
                innerRadius: '103%'
            }]
        },

        // the value axis
        yAxis: {
            min: -90,
            max: 90,

            minorTickInterval: 'auto',
            minorTickWidth: 1,
            minorTickLength: 10,
            minorTickPosition: 'inside',
            minorTickColor: '#666',

            tickPixelInterval: 30,
            tickWidth: 2,
            tickPosition: 'inside',
            tickLength: 10,
            tickColor: '#666',
            labels: {
                step: 2,
                rotation: 'auto'
            },            
            plotBands: [{
                from: -90,
                to: -30,
                color: '#55BF3B' // green
            }, {
                from: -30,
                to: 30,
                color: '#DDDF0D' // yellow
            }, {
                from: 30,
                to: 90,
                color: '#DF5353' // red
            }]
        },
		credits: {
			enabled: false
		},
        series: [{
            name: 'Speed',
            data: [150],
            tooltip: {
                valueSuffix: ''
            }
        }]

    },
        // Add some life
        function (chart) {
            if (!chart.renderer.forExport) {
                setInterval(function () {
                    var point = chart.series[0].points[0],
                        newVal,                        
                    newVal = point.y - point.y + g_pitchAngle;
                    
                    point.update(newVal);

                }, 500);
            }
        });
}

function doSearch(ac,map) {
    var point;
    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
    var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }    
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }    
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
    var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;



        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
        
        // setPlace(map);
        // map.clearOverlays();    //清除地图上所有覆盖物
        var pointB;
        var allOverlay = map.getOverlays();
        for (var i = 0; i < allOverlay.length -1; i++){
            if(allOverlay[i].getLabel().content == "起始位置"){
                map.removeOverlay(allOverlay[i]);
            }
            if(allOverlay[i].getLabel().content == "结束位置"){
                pointB = allOverlay[i].getPoi(0);
                alert(pointB.lng);
            }
        }
    

        function myFun(){
             
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            var label = new BMap.Label("起始位置",{offset:new BMap.Size(20,-10)});
            pointA = pp;
        
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp,label));    //添加标注
            if (pointB != undefined ) {
                var polyline = new BMap.Polyline([pointA,pointB], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});  //定义折线
                map.addOverlay(polyline);  
            }

        }
        var local = new BMap.LocalSearch(map, { //智能搜索
          onSearchComplete: myFun
        });
        local.search(myValue); 
    });
}



function doSearch_end(ac,map) {
    var point;
    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
    var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }    
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }    
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
    var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;

         alert("aaaaaaaaaa");

        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;


        
        // setPlace(map);
        // map.clearOverlays();    //清除地图上所有覆盖物
        var pointB;
        var allOverlay = map.getOverlays();
         alert(allOverlay[1].getPosition().lng);
        for (var i = 0; i < allOverlay.length -1; i++){
            if(allOverlay[i].getLabel().content == "结束位置"){
               
                map.removeOverlay(allOverlay[i]);

            }
            // if(allOverlay[i].getLabel().content == "开始位置"){
            //      alert("aaaaaaaaaa");
            //     pointA = allOverlay[i].getPoi(0);
            //     alert(pointA.lng);
            // }
        }
    

        function myFun(){
             
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            var label = new BMap.Label("结束位置",{offset:new BMap.Size(20,-10)});
            pointB = pp;
        
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp,label));    //添加标注
            if (pointA != undefined ) {
                var polyline = new BMap.Polyline([pointA,pointB], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});  //定义折线
                map.addOverlay(polyline);  
            }

        }
        var local = new BMap.LocalSearch(map, { //智能搜索
          onSearchComplete: myFun
        });
        local.search(myValue); 
    });
}

function makeLine(map,pointA,pointB) {
    var polyline = new BMap.Polyline([pointA,pointB], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});  //定义折线
    map.addOverlay(polyline);  
}

function G(id) {
        return document.getElementById(id);
    }

function deletePoint(map){
        var allOverlay = map.getOverlays();
        for (var i = 0; i < allOverlay.length -1; i++){
            if(allOverlay[i].getLabel().content == "我是id=1"){
                map.removeOverlay(allOverlay[i]);
                return false;
            }
        }
    }